package com.want.evenchat.model.dao;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.want.evenchat.model.Model;
import com.want.evenchat.model.bean.GroupInfo;
import com.want.evenchat.model.bean.InvitionInfo;
import com.want.evenchat.model.bean.UserInfo;
import com.want.evenchat.model.db.DBHelper;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Want on 2018/5/6.
 */

public class InviteTableDAO {

    //邀请信息表的操作类

    private DBHelper mHelper;

    public InviteTableDAO(DBHelper dbHelper) {
        mHelper = dbHelper;
    }


    // 添加邀请
    public void addInvitation(InvitionInfo invitationInfo) {
        // 获取数据库链接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行添加语句
        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_REASON , invitationInfo.getReason());//原因
        values.put(InviteTable.COL_STATUS , invitationInfo.getStatus().ordinal());//状态

        UserInfo user = invitationInfo.getUser();

        if (user != null){//联系人
            values.put(InviteTable.COL_USER_HXID , invitationInfo.getUser().getHxid());
            values.put(InviteTable.COL_USER_NAME , invitationInfo.getUser().getName());
        }else {//群组
            values.put(InviteTable.COL_GROUP_HXID , invitationInfo.getGroup().getGroupId());
            values.put(InviteTable.COL_GROUP_NAME , invitationInfo.getGroup().getGroupName());
            values.put(InviteTable.COL_USER_HXID , invitationInfo.getGroup().getInvatePerson());
        }

        db.replace(InviteTable.TAB_NAME , null , values);

    }

    // 获取所有邀请信息
    public List<InvitionInfo> getInvitations() {
        //获取数据库连接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行查询语句
        String sql = "select * from " + InviteTable.TAB_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        List<InvitionInfo> invitionInfos = new ArrayList<InvitionInfo>();

        while (cursor.moveToNext()){
            InvitionInfo invitionInfo = new InvitionInfo();

            invitionInfo.setReason(cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON)));//原因
            invitionInfo.setStatus(int2InviteStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS))));//

            String groupId = cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID));

            if (groupId == null){//如股为空就是联系人的邀请信息
                UserInfo userInfo = new UserInfo();

                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                userInfo.setName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                userInfo.setNick(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));

                invitionInfo.setUser(userInfo);

            }else{//群组的邀请信息

                GroupInfo groupInfo = new GroupInfo();

                groupInfo.setGroupId(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID)));
                groupInfo.setGroupName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_NAME)));
                groupInfo.setInvatePerson(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));

                invitionInfo.setGroup(groupInfo);

            }

            //添加本次循环的邀请信息到总的集合中
            invitionInfos.add(invitionInfo);

        }

        //关闭资源
        cursor.close();

        //返回数据
        return invitionInfos;
    }

    // 将int类型状态转换为邀请的状态
    private InvitionInfo.InvitationStatus int2InviteStatus(int intStatus) {
        
        if (intStatus == InvitionInfo.InvitationStatus.NEW_INVITE.ordinal()) {
            return InvitionInfo.InvitationStatus.NEW_INVITE;
        }

        if (intStatus == InvitionInfo.InvitationStatus.INVITE_ACCEPT.ordinal()) {
            return InvitionInfo.InvitationStatus.INVITE_ACCEPT;
        }

        if (intStatus == InvitionInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER.ordinal()) {
            return InvitionInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER;
        }

        if (intStatus == InvitionInfo.InvitationStatus.NEW_GROUP_INVITE.ordinal()) {
            return InvitionInfo.InvitationStatus.NEW_GROUP_INVITE;
        }

        if (intStatus == InvitionInfo.InvitationStatus.NEW_GROUP_APPLICATION.ordinal()) {
            return InvitionInfo.InvitationStatus.NEW_GROUP_APPLICATION;
        }

        if (intStatus == InvitionInfo.InvitationStatus.GROUP_INVITE_ACCEPTED.ordinal()) {
            return InvitionInfo.InvitationStatus.GROUP_INVITE_ACCEPTED;
        }

        if (intStatus == InvitionInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED.ordinal()) {
            return InvitionInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED;
        }

        if (intStatus == InvitionInfo.InvitationStatus.GROUP_INVITE_DECLINED.ordinal()) {
            return InvitionInfo.InvitationStatus.GROUP_INVITE_DECLINED;
        }

        if (intStatus == InvitionInfo.InvitationStatus.GROUP_APPLICATION_DECLINED.ordinal()) {
            return InvitionInfo.InvitationStatus.GROUP_APPLICATION_DECLINED;
        }

        if (intStatus == InvitionInfo.InvitationStatus.GROUP_ACCEPT_INVITE.ordinal()) {
            return InvitionInfo.InvitationStatus.GROUP_ACCEPT_INVITE;
        }

        if (intStatus == InvitionInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION.ordinal()) {
            return InvitionInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION;
        }

        if (intStatus == InvitionInfo.InvitationStatus.GROUP_REJECT_APPLICATION.ordinal()) {
            return InvitionInfo.InvitationStatus.GROUP_REJECT_APPLICATION;
        }

        if (intStatus == InvitionInfo.InvitationStatus.GROUP_REJECT_INVITE.ordinal()) {
            return InvitionInfo.InvitationStatus.GROUP_REJECT_INVITE;
        }

        return null;

    }

    // 删除邀请
    public void removeInvitation(String hxId) {
        if (hxId == null){
            return;
        }

        //1.获取数据库连接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //2.执行删除语句
        db.delete(InviteTable.TAB_NAME , InviteTable.COL_USER_HXID + " =?" , new String[]{hxId});
    }

    // 更新邀请状态
    public void updateInvitationStatus(InvitionInfo.InvitationStatus invitationStatus, String hxId) {
        if (hxId == null){
            return;
        }

        //1.获取数据库连接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //2.执行更新操作
        ContentValues values = new ContentValues();
        values.put(InviteTable.COL_STATUS , invitationStatus.ordinal());

        db.update(InviteTable.TAB_NAME , values , InviteTable.COL_USER_HXID + " =?" , new String[]{hxId});

    }

}
